﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class PurchaseOrder : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
            LoadData();
    }
    //protected void btnDashboard_Click(object sender, EventArgs e)
    //{
    //    Response.Redirect("~/WorkFlowDemo/WorkFlowDashBoard.aspx");
    //}
    protected void btnSave_Click(object sender, EventArgs e)
    {
        new PurchaseOrderController().SavePurchaseOrder(txtItemName.Text, Convert.ToDateTime(txtOderDate.Text), Convert.ToInt64(txtItemQuantity.Text), Convert.ToDecimal(txtPrice.Text), chkCredit.Checked, Session["usersession"].ToString());
        pnlRol.Visible = false;
        LoadData();
    }
    protected void btnCreatePurchaseOrder_Click(object sender, EventArgs e)
    {
        pnlRol.Visible = true;
        btnSubmit.Visible = false;
    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        new PurchaseOrderController().SubmitPurchaseOrder(new PurchaseOrderInfo
        {
            PurchaseOrderId = Convert.ToInt64(lblOderId.Text),
            ItemName = txtItemName.Text,
            Itemquantity = Convert.ToInt64(txtItemQuantity.Text),
            PurchaseOrderDate = Convert.ToDateTime(txtOderDate.Text),
            WorkFlowDate = DateTime.Now,
            WorkflowInstanceId = Convert.ToInt32(lblInstanceId.Text),
            Price = Convert.ToDecimal(txtPrice.Text),
            IsCredit = chkCredit.Checked,
        }, Session["usersession"].ToString());
        pnlRol.Visible = false;
        LoadData();
    }
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        pnlRol.Visible = false;
    }


    private void LoadData()
    {
        List<PurchaseOrderInfo> lst = new PurchaseOrderController().PurchaseOrdersByStateName("PurchaseOrderCreation");
        grdWorkFlows.DataSource = lst;
        grdWorkFlows.DataBind();
    }
    public void grdWorkFlows_RowCommand(object sender, CommandEventArgs e)
    {
        int workflowInstanceId = Convert.ToInt32(e.CommandArgument.ToString());
        var purchaseOrderInfo = new PurchaseOrderController().PurchaseOrdersByWorkFlowInstanceId(workflowInstanceId);
        lblOderId.Text = purchaseOrderInfo.PurchaseOrderId.ToString();
        lblInstanceId.Text = purchaseOrderInfo.WorkflowInstanceId.ToString();
        txtItemName.Text = purchaseOrderInfo.ItemName;
        txtItemQuantity.Text = purchaseOrderInfo.Itemquantity.ToString();
        txtOderDate.Text = purchaseOrderInfo.PurchaseOrderDate.ToString();
        chkCredit.Checked = purchaseOrderInfo.IsCredit;
        txtPrice.Text = purchaseOrderInfo.Price.ToString();
        pnlRol.Visible = true;
        btnSubmit.Visible = true;
    }
}
